package com.grad.webstudy;

import com.grad.webstudy.annota.resolver.ApiAuthorizationInterceptor;
import com.grad.webstudy.annota.resolver.LoginAuthorizationInterceptor;
import com.grad.webstudy.annota.resolver.CurrentUserArgumentResolver;
import com.grad.webstudy.constants.FileContants;
import com.grad.webstudy.constants.UserContants;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import java.util.List;

@SpringBootApplication
@MapperScan("com.grad.webstudy.dao")
@EnableTransactionManagement
public class WebstudyApplication implements WebMvcConfigurer {

    public static void main(String[] args) {
        System.setProperty("es.set.netty.runtime.available.processors","false");
        SpringApplication.run(WebstudyApplication.class, args);
    }

    @Bean
    CurrentUserArgumentResolver currentUserArgumentResolver() {
        return new CurrentUserArgumentResolver();
    }


    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(currentUserArgumentResolver());
        WebMvcConfigurer.super.addArgumentResolvers(argumentResolvers);
    }

    @Bean
    LoginAuthorizationInterceptor authorizationInterceptor() {
        return new LoginAuthorizationInterceptor();
    }

    @Bean
    ApiAuthorizationInterceptor apitoAuthorizationInterceptor() {
        return new ApiAuthorizationInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器组成一个拦截器链
        // addPathPatterns 用于添加拦截规则
        // excludePathPatterns 用于排除拦截
        registry.addInterceptor(authorizationInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(apitoAuthorizationInterceptor()).addPathPatterns("/**");

        WebMvcConfigurer.super.addInterceptors(registry);
    }


    /**
     * 添加资源映射配置，方便网络访问资源
     *
     * @param registry
     */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler(FileContants.FILE_MAP).addResourceLocations("file:" + FileContants.USER_IMAGE_PATH,
                "file:" + FileContants.COURSE_COVER_PATH,
                "file:" + FileContants.COURSE_VIDEO_PATH);
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

}
